package com.ruoyi.zq.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.zq.domin.ZqEntryBlank;
import com.ruoyi.common.zq.domin.ZqVolunteerActivity;
import com.ruoyi.common.zq.domin.dto.ListApplyDTO;
import com.ruoyi.common.zq.domin.vo.EntryBlankVO;
import com.ruoyi.zq.domin.dto.GradeDTO;
import com.ruoyi.zq.domin.dto.ZqNoticeDTO;
import com.ruoyi.zq.service.EntryBlankService;
import com.ruoyi.zq.service.VolunteerActivityService;
import com.ruoyi.zq.service.ZqNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.sql.rowset.serial.SerialException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @Author: LoneyFeather
 * @Date: Created in 15:19 2021/12/28
 */

@Api(tags = "志愿活动报名表以及相应的评价")
@RestController
@RequestMapping("/entryBlank")
public class EntryBlankController extends BaseController {

    @Autowired
    private EntryBlankService entryBlankService;

    @Autowired
    private VolunteerActivityService activityService;

    @Autowired
    private ZqNoticeService zqNoticeService;


    @PreAuthorize("@ss.hasPermi('zq:volunteer:getinfo')")
    @ApiOperation("活动报名关于状态的通用接口")
    @PostMapping("/apply")
    public AjaxResult add(@Validated @RequestBody ZqEntryBlank entryBlankDTO) throws SerialException {
        entryBlankDTO.setUserId(getUserId());
        return toAjax(entryBlankService.applyByPitchOn(entryBlankDTO));
    }

//    @PreAuthorize("@ss.hasPermi('zq:entryblank:getInfo')")
    @ApiOperation("活动报名详情")
    @GetMapping("/getInfo")
    public AjaxResult getInfo(ZqEntryBlank entryBlankDTO) throws SerialException {
        entryBlankDTO.setUserId(getUserId());
        ZqEntryBlank entryBlank = entryBlankService.getInfo(entryBlankDTO);
        if (ObjectUtils.isEmpty(entryBlank)){
            entryBlank = new ZqEntryBlank();
            BeanUtils.copyProperties(entryBlankDTO,entryBlank);
        }
        return AjaxResult.success(entryBlank);
    }

    @PreAuthorize("@ss.hasPermi('zq:entryblank:list')")
    @ApiOperation("查询报名情况")
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "activityId",required = true) Long activityId){

//        @RequestParam(value = "pitchOn",required = true) Integer pitchOn
        if (!activityService.controlByActivityId(activityId)){
            throw new ServiceException("没有该活动的权限");
        }
//        startPage();
        List<EntryBlankVO> list = entryBlankService.
                listByActivityId(activityId);
        return AjaxResult.success(list);
    }


//    @PreAuthorize("@ss.hasPermi('zq:entryblank:list')")
    @ApiOperation("评价时查询报名情况")
    @PostMapping("/listEval")
    public TableDataInfo listEval(@RequestBody ZqEntryBlank zqEntryBlankDTO){
        if (!activityService.controlByActivityId(zqEntryBlankDTO.getActivityId())){
            throw new ServiceException("没有该活动的权限");
        }
        if(zqEntryBlankDTO.getPitchOn() == null) {
            zqEntryBlankDTO.setPitchOn(1);
        }
        startPage();
        List<EntryBlankVO> list = entryBlankService.listEntryBlank(zqEntryBlankDTO);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('zq:entryblank:grade')")
    @ApiOperation("添加评语")
    @PostMapping("/grade")
    public AjaxResult grade(@RequestBody GradeDTO gradeDTO){
        if (!activityService.controlByActivityId(gradeDTO.getActivityId())){
            throw new ServiceException("没有该活动的权限");
        }
        return toAjax(entryBlankService.gradeByModel(gradeDTO));
    }

    @PreAuthorize("@ss.hasPermi('zq:entryblank:listApply')")
    @ApiOperation("活动报名选择批量")
    @PostMapping("listApply")
    public AjaxResult listApply(ListApplyDTO listApplyDTO){
        Long activityId = listApplyDTO.getActivityId();
        Long[] userIds = listApplyDTO.getUserIds();

        if (!activityService.controlByActivityId(activityId)){
            throw new ServiceException("没有该活动的权限");
        }
        // 大致逻辑，因为穿梭框的原因，将已选中的数据一次性进行保存，所以先将所有的选中修改为未选中，再进行保存。通知详情也是这样
        //将所有修改为未选中：0
        int num = entryBlankService.updatePitchOnByActivityId(activityId);

        // 批量添加通知的数据
        ZqVolunteerActivity activity = activityService.getById(activityId);
        ZqNoticeDTO noticeDTO = zqNoticeService.getNoticePithOnTelephone(activity.getActivityName());
        noticeDTO.setUserIds(userIds);
        zqNoticeService.addNoticeAndDetails(noticeDTO);

        if(ObjectUtils.isNotEmpty(userIds)) {
            List<ZqEntryBlank> zqEntryBlankList = new ArrayList<>();
            for (Long userId : userIds) {
                ZqEntryBlank ob = entryBlankService.updateOnModel(activityId, userId);
                zqEntryBlankList.add(ob);
            }
            logger.info("志愿批量入选通知：{}", noticeDTO.toString());

            return AjaxResult.success(entryBlankService.saveOrUpdateBatch(zqEntryBlankList));
        }else {
            return AjaxResult.success("所有选中人员已取消");
        }
    }


}
